package myThread;

public class Cooker extends Thread{
    private Desk desk;

    public Cooker(Desk desk) {
        this.desk = desk;
    }
    /*1. 判断临界区是否有资源
    * 2. 若有就等待，没有就生产
    * 3. 判断是否达到生产资源上限
    * 4. 临界区资源加一*/

    @Override
    public void run() {
        while(true){
            synchronized(desk.getLock()){
                if(desk.getCount() == 0) {
                    break;
                }else{
                    if (desk.isFlag()) {// 临界区有资源就等待
                        try {
                            desk.getLock().wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {// 临界区没有资源就生产
                        System.out.println("生产者创建资源！");
                        desk.setFlag(true);
                        desk.getLock().notifyAll();
                    }
                }
            }
        }
    }
}
